package module{
	import component.Image;
	
	import flash.display.BitmapData;
	
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Sphere;
	
	import utils.Tween;
	
	public class Ball extends DisplayObject3D{
		
		private var sphere:Sphere;
		private var _numberOfVerts:uint;
		public var blockList:Vector.<Block>;
		
		public var src:String = "resource/pic%i.jpg";
		
		public var open:Boolean;
		
		public function Ball(){
			this.sphere = new Sphere(null,Global.BALL_RADIUS,Global.BALL_SEGMENTSW,Global.BALL_SEGMENTSH);
			this._numberOfVerts = sphere.geometry.vertices.length;
			this.blockList = new Vector.<Block>;
			initBlock();
		}
		
		private function initBlock():void{
			for (var i:uint= 1; i < _numberOfVerts-1; i++){
				var url:String = src.replace("%i",i);
				var asset:BitmapData = Image.getDataFromPool(url);
				if(asset == null)asset = Image.getDataFromPool(src.replace("%i",int(Math.random()*11 + 1)));
				var materia:BitmapMaterial = new BitmapMaterial(asset);
				materia.smooth = true;
				var plane:Block = new Block(materia,asset.width,asset.height);
				plane.oW = asset.width;
				plane.oH = asset.height;
				plane.oX = plane.x = sphere.geometry.vertices[i].x;
				plane.oY = plane.y = sphere.geometry.vertices[i].y;
				plane.oZ = plane.z = sphere.geometry.vertices[i].z;
				
				if (Math.abs(plane.z) < 0.00001){
					if (plane.x > 0){
						plane.rotationX=Math.atan2(plane.y, plane.x) * 180 / Math.PI;
						plane.rotationY=-90;
					}else{
						plane.rotationX=180 - Math.atan2(plane.y, plane.x) * 180 / Math.PI;
						plane.rotationY=90;
					}
				}else if (plane.z > 0){
					plane.rotationX=Math.atan2(plane.y,  Math.sqrt(plane.z*plane.z+plane.x*plane.x)) * 180 / Math.PI;
					plane.rotationY=180 + Math.atan2(plane.x, plane.z) * 180 / Math.PI;
				}else	{
					plane.rotationX=180 - Math.atan2(plane.y, -Math.sqrt(plane.z*plane.z+plane.x*plane.x)) * 180 / Math.PI;
					plane.rotationY=180 + Math.atan2(plane.x, plane.z) * 180 / Math.PI;
				}
				plane.oRX = plane.rotationX;
				plane.oRY = plane.rotationY;
				plane.oRZ = plane.rotationZ;
				blockList.push(plane);
				plane.material.doubleSided = true;
				this.addChild(plane);
			}
		}
		
		private const angles:Array = [90,210,330];
		public function checkIsOpenBall():void{
			var col:int = angles.indexOf(Math.floor(this.rotationY));
			if(col<0)return;
			if(open){
				for(var i:int = 0;i<this.blockList.length;i++ ){
					var plane:Block = this.blockList[i];
					if((i-col)%3==0 && plane.visible){
						plane.visible = false;
						Map.instance.tweenGrid(plane.material.bitmap);
					}
				}
			}else{
				
			}
		}
		
		
		private const LEADW:int = 60; //左右边距
		private const LEADH:int = 40; //上下边距
		public var isSurface:Boolean;
		/**
		 * 平面动画效果
		 * */ 
		public function surfaceEffect(isOpen:Boolean):void{
			this.isSurface = isOpen;
			var col:int = Math.ceil(this.rotationY/120);
			for(var i:int = 0;i< blockList.length;i++){
				var plane:Block = blockList[i];
				var dX:Number;
//				var dX:Number = (i%3)*(plane.oW + LEADW) - plane.oW;
				var dY:Number = int(i/3)*(plane.oH + LEADH) - plane.oH;
				if(isOpen){
					//放在中间列
					if((i-(col-1))%3==0){
						dX = (plane.oW + LEADW) - plane.oW;
					}
					//放在右边
					else if((i-col)%3==0){
						dX =  2*(plane.oW + LEADW) - plane.oW;
					}else{
						dX = - plane.oW;
						
					}
				} 
				
				var oX:int = plane.x;
				var oY:int = plane.y;
				var oZ:int = plane.z;
				
				var oRX:int = plane.rotationX;
				var oRY:int = plane.rotationY;
				var oRZ:int = plane.rotationZ;
				
				var dRX:int = oRX>180?360:0;
				var dRY:int = oRY>180?360:0;
				var dRZ:int = oRZ>180?360:0;
				
				if(isOpen){
					Tween.to(plane,500,{
						rotationX:[oRX,dRX],rotationY:[oRY,dRY],x:[oX,dX],z:[oZ,0],y:[oY,dY]
					});
				}else{
					Tween.to(plane,600,{x:[oX,plane.oX],y:[oY,plane.oY],z:[oZ,plane.oZ],
						rotationX:[dRX,plane.oRX],rotationY:[dRY,plane.oRY]});
				}
			}
		}
	}
}